﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;

namespace Web.Shalo.BanPMS1.Admin
{
    public partial class DDDChuaGiao : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["manv"] == null || Session["matkhau"] == null)
            {
                Response.Redirect("Default.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    HienThiDDChuaGiao();
                    MultiView1.ActiveViewIndex = 0;
                }
            }
        }

        public void DDLNhanVien()
        {
            NhanVienController obj = new NhanVienController();
            string manv = "";
            ddlNhanVien.DataSource = obj.bllHienThiNV(manv);
            ddlNhanVien.DataTextField = "TenNV";
            ddlNhanVien.DataValueField = "MaNV";
            ddlNhanVien.DataBind();
        }

        public void HienThiDDChuaGiao()
        {
            HoaDonController obj = new HoaDonController();
            GVDonDH.DataSource = obj.bllHienThiDDChuaGiao();
            GVDonDH.DataBind();
        }

        protected void GVDonDH_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            int sohd = Convert.ToInt32(GVDonDH.Rows[e.NewSelectedIndex].Cells[0].Text);
            MultiView1.ActiveViewIndex = 1;
            HienThiTT(sohd);
        }

        public void HienThiTT(int sohd)
        {
            HoaDonController obj = new HoaDonController();
            DataTable tt = obj.bllHienThiTTDDChuaGiao(sohd);
            labSoHD.Text = tt.Rows[0]["SoHD"].ToString();
            labNgayLap.Text = tt.Rows[0]["Ngaylap"].ToString();
            labTenKH.Text = tt.Rows[0]["TenKH"].ToString();
            labNgaySinh.Text = tt.Rows[0]["NgaySinh"].ToString();
            labDiaChi.Text = tt.Rows[0]["DiaChi"].ToString();
            labSoDT.Text = tt.Rows[0]["SoDT"].ToString();
            labGhiChu.Text = tt.Rows[0]["GhiChu"].ToString();
            labTongTien.Text = tt.Rows[0]["TongTien"].ToString();

            DDLNhanVien();
            CTHDController objcthd = new CTHDController();
            GVCthd.DataSource = objcthd.bllHienThiMHChuaGiao(sohd);
            GVCthd.DataBind();
        }

        protected void btnGiaoHang_Click(object sender, EventArgs e)
        {
            bool res = false;
            PhieuGHController obj = new PhieuGHController();
            HoaDonController objhd = new HoaDonController();
            res = objhd.bllSuaHD(Convert.ToInt32(labSoHD.Text));
            res = obj.bllThemPGH(Convert.ToInt32(labSoHD.Text), ddlNhanVien.SelectedValue.ToString(), lichNgayGiao.SelectedDate.ToString());
            ThongBao(res);
        }
        public void ThongBao(bool res)
        {
            if (res)
            {
                Response.Redirect("DDDChuaGiao.aspx");
            }
            else
            {
                Page.RegisterClientScriptBlock("mes", "<script language='javascript'>alert('Thất Bại')</script>");
            }

        }

        protected void btnDSDonDH_Click(object sender, EventArgs e)
        {
            Response.Redirect("DDDChuaGiao.aspx");
        }

        protected void GVDonDH_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVDonDH.PageIndex = e.NewPageIndex;
            HienThiDDChuaGiao();
        }
    }
}